//
//  WHEnterpriseGovernmentVC.m
//  WHEasyBizLink
//
//  Created by shengmeng chen on 2024/12/4.
//

#import "WHEnterpriseGovernmentVC.h"
#import "WHEnterpriseGovernmentCell.h"
#import "WHEnterpriseGovernmentModel.h"
#import "WHCompanyInfoApplyVC.h"
#import "SDAutoLayout.h"
#import "WHTdsSelectVC.h"


@interface WHEnterpriseGovernmentVC ()

@property (nonatomic,strong)UIBarButtonItem *rightItem;


@end

@implementation WHEnterpriseGovernmentVC
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationController.navigationBar.topItem.title = @"政府单位";
    
}

#pragma mark - loadDatas
- (void)loadDatas{
    [super loadDatas];
    [WHEnterpriseGovernmentModel fetchDatasSuccessBlock:^(id  _Nonnull models) {
        self.datas =[NSMutableArray arrayWithArray:models];
    }];

}
#pragma mark - setupUI
- (void)setupUI{
    [super setupUI];
    //一个简单的super setupUI = createTableView
    self.tableView.frame =CGRectMake(0,navHeight1+40,ScreenW,ScreenH-navHeight1-DefaultBottomTabBarHeight-40);
    
    self.navigationItem.rightBarButtonItem =self.rightItem;
    
}
-(void)loadMoreDatas
{
    [super loadMoreDatas];
}

-(void)rightBarButtonItemClicked
{
//    WHTdsSelectVC*vc =[[WHTdsSelectVC alloc]init];
//    vc.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - <tableViewDelegate>
- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    WHEnterpriseGovernmentCell *cell = [WHEnterpriseGovernmentCell
                                     createReuseIdentifier:@"WHEnterpriseGovernmentCellID" WithTableView:tableView];
    cell.model = self.datas[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView
 heightForRowAtIndexPath:(NSIndexPath *)indexPath{

   // return [tableView cellHeightForIndexPath:indexPath model:self.datas[indexPath.row] keyPath:@"model" cellClass:[WHEnterpriseCompanyCell class] contentViewWidth:ScreenW];
    return kMarginH*10;
}

-(void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
   
}


- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive
                                                                       title:@"删除"
                                                                       handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        // 处理删除操作
        // 例如，从数据源中移除对应的元素，并调用tableView的deleteRowsAtIndexPaths方法
        // 这里仅作示例，不展示完整的数据源修改和UI更新代码
        NSLog(@"Delete action triggered for row %ld", (long)indexPath.row);
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"是否确定要删除" message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAtcion =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        UIAlertAction *modifytcion =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
            [self.datas removeObjectAtIndex:indexPath.row];
            [self.tableView reloadData];
        }];
        [alertC addAction:cancelAtcion];
        [alertC addAction:modifytcion];
        
        [self presentViewController:alertC animated:YES completion:nil];
        completionHandler(YES); // 通知系统已完成操作
    }];

    // 设置删除操作的图标
   // deleteAction.image = [UIImage imageNamed:@"delete_icon"];
    deleteAction.backgroundColor = [UIColor redColor];
    
    
    WHEnterpriseGovernmentModel*model = self.datas[indexPath.row];
    NSString *title       = @"停用";
    NSString *alertString = @"是否确定要停用";
    UIColor  *color  =[UIColor greenColor];
    if (model.isRun ==NO) {
                  title = @"启用";
        alertString     = @"是否确定要启用";
        color           = [UIColor lightGrayColor];
    }
    
    UIContextualAction *action0 =[UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:title handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:alertString message:@"" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAtcion =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        UIAlertAction *modifytcion =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            model.isRun = !model.isRun;
            [tableView reloadData];
        }];
        [alertC addAction:cancelAtcion];
        [alertC addAction:modifytcion];
        [self presentViewController:alertC animated:YES completion:nil];
       
        completionHandler(YES);
    }];
    
    
    action0.backgroundColor = color;
    
    // 创建UISwipeActionsConfiguration对象并添加操作
    UISwipeActionsConfiguration *configuration = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction,action0]];
    return configuration;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DebugLog(@"tableView = %ld",(long)indexPath.row);
}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

#pragma mark - <searchViewDelegate>
-(void)searchTileViewTextFieldDidChange:(NSString *)textFieldString
{
    
}
-(void)searchTileViewSearchButtonClickSearchString:(NSString *)searchString
{
}

-(UIBarButtonItem *)rightItem
{
    if (!_rightItem)
    {
        UIButton* rightBtn= [[UIButton alloc] initWithFrame:CGRectMake(0,0,25,25)];
      [rightBtn addTarget:self action:@selector(rightBarButtonItemClicked)forControlEvents:UIControlEventTouchUpInside];
        [rightBtn setBackgroundImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
        _rightItem =[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    }
    return _rightItem;
}


@end
